package com.ja.openchat.ui.chat;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

import androidx.core.content.ContextCompat;

import com.blankj.utilcode.util.ToastUtils;
import com.gyf.immersionbar.ImmersionBar;
import com.ja.openchat.R;
import com.lqr.emoji.EmojiManager;
import com.lqr.emoji.EmotionLayout;
import com.lqr.emoji.IEmotionSelectedListener;
import com.lqr.emoji.LQREmotionKit;
import com.lqr.emoji.StickerManager;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.interfaces.OnSelectListener;
import com.lxj.xpopup.util.XPopupUtils;
import com.vachel.editor.PictureEditActivity;
import com.zy.openchat.core.model.sticker.StickerInfo;
import com.zy.openchat.util.SingleMediaScanner;

import java.io.File;
import java.io.FileNotFoundException;

public class GoChatPicEditActivity extends PictureEditActivity {
    private int mImageType = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        View tv_cancel = findViewById(R.id.tv_cancel);
        ((ViewGroup.MarginLayoutParams)tv_cancel.getLayoutParams()).topMargin = ImmersionBar.getStatusBarHeight(this);
        mImageType = getIntent().getIntExtra(EXTRA_TYPE,0);
    }

    @Override
    public void initData() {
        mSupportEmoji = true;
    }

    @Override
    public View getStickerLayout() {
        EmotionLayout emotionLayout = new EmotionLayout(this);
        emotionLayout.setBackgroundColor(ContextCompat.getColor(this, R.color.white));
        emotionLayout.setEmotionSelectedListener(new IEmotionSelectedListener() {
            @Override
            public void onEmojiSelected(String key) {
                onImage(XPopupUtils.dp2px(GoChatPicEditActivity.this,40),XPopupUtils.dp2px(GoChatPicEditActivity.this,40)).setImageDrawable(EmojiManager.getDrawable(GoChatPicEditActivity.this,Integer.decode(key)));
            }

            @Override
            public void onStickerSelected(StickerInfo stickerInfo) {
                String stickerBitmapUri = StickerManager.getInstance().getStickerBitmapUri(stickerInfo);
                LQREmotionKit.getImageLoader().displayImage(GoChatPicEditActivity.this, stickerBitmapUri, onImage(XPopupUtils.dp2px(GoChatPicEditActivity.this,60),XPopupUtils.dp2px(GoChatPicEditActivity.this,60)));
            }

            @Override
            public void onStickerRemoved(StickerInfo stickerInfo) {

            }
        });
        return emotionLayout;
    }




    @Override
    protected void onDestroy() {
        super.onDestroy();
    }


    private int clickDonePosition = -1;

    @Override
    protected void clickDone() {
        clickDonePosition = -1;
        if(mImageType==1) {
            new XPopup.Builder(GoChatPicEditActivity.this).asBottomList(null, new String[]{getString(R.string.send_to_friend), getString(R.string.save_picture),getString(R.string.cancle_text)}, new OnSelectListener() {
                @Override
                public void onSelect(int position, String text) {
                    clickDonePosition = position;
                    mPictureEditView.saveEdit(GoChatPicEditActivity.this);
                }
            }).show();
        }else{
            mPictureEditView.saveEdit(this);
        }

    }

    @Override
    public void onSaveSuccess(String savePath) {
        if(mImageType==1){
            switch (clickDonePosition){
                case 0:
                    Intent intent = new Intent(GoChatPicEditActivity.this, ForwardActivity.class);
                    intent.putExtra(ForwardActivity.BUNDLE_OUTER_PATH,savePath);
                    startActivity(intent);
                    break;
                case 1:
                    if (Build.VERSION.SDK_INT < 29) {
                        try {
                            MediaStore.Images.Media.insertImage(getContentResolver(), savePath, getResources().getString(R.string.app_name), getResources().getString(R.string.app_name));
                        } catch (FileNotFoundException e) {
                            e.printStackTrace();
                        }
                    }
                    new SingleMediaScanner(getApplicationContext(), savePath, new SingleMediaScanner.ScanListener() {
                        @Override
                        public void onScanFinish() {
                            ToastUtils.showShort(getString(R.string.save_album_success));
                        }
                    });

                    break;
                default:

                    break;

            }
        }else{
            Intent intent = new Intent();
            intent.putExtra(EXTRA_IMAGE_URI, Uri.fromFile(new File(savePath)));
            setResult(Activity.RESULT_OK,intent);
            finish();
        }
    }
}
